using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node function_definition in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnfunction_definition : Vnexternal_declaration {
        // [207]=function_definition : declaration_specifiers declarator declaration_list compound_statement ;
        // [208]=function_definition : declaration_specifiers declarator compound_statement ;
        // [209]=function_definition : declarator declaration_list compound_statement ;
        // [210]=function_definition : declarator compound_statement ;

        private readonly Vndeclaration_specifiers? r3;
        private readonly Vndeclarator r2;
        private readonly Vndeclaration_list? r1;
        private readonly Vncompound_statement r0;

        private readonly TokenRange _scope;
        public override TokenRange Scope => _scope;

        public override void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public override void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            if (r3 != null) {
                context.PrintBlanksAnd(r3, preConfig, writer);
                context.PrintCommentsBetween(r3, r2, config, writer);
            }
            context.PrintBlanksAnd(r2, r3 == null ? preConfig : config, writer);
            if (r1 != null) {
                context.PrintCommentsBetween(r2, r1, config, writer);
                context.PrintBlanksAnd(r1, config, writer);
                context.PrintCommentsBetween(r1, r0, config, writer);
            }
            else {
                context.PrintCommentsBetween(r2, r0, config, writer);
            }
            context.PrintBlanksAnd(r0, config, writer);
        }

        public override IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        internal Vnfunction_definition(Vndeclaration_specifiers? r3, Vndeclarator r2, Vndeclaration_list? r1, Vncompound_statement r0) {
            this.r3 = r3;
            this.r2 = r2;
            this.r1 = r1;
            this.r0 = r0;
            if (r3 != null) { this._scope = new TokenRange(r3, r0); }
            else { this._scope = new TokenRange(r2, r0); }
        }

    }
}
